//
//  UIButton+Extention.swift
//  trucks
//
//  Created by 刘泽 on 2017/12/4.
//  Copyright © 2017年 刘泽. All rights reserved.
//

import Foundation
import UIKit

public typealias BtnBlock = (UIButton)->()

extension UIButton {

//    //MARK:设置网络图片
//    public func setImage(url:String?,
//                  placeHolder:UIImage? = nil,
//                  state:UIControlState){
//        if url == nil || url!.isEmpty{
//            self.setImage(placeHolder, for: state)
//        }else{
//            let urlStr = url!.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
//
//            let resource = ImageResource.init(downloadURL: URL.init(string: urlStr!)!)
//            kf.setImage(with: resource, for: state)
//        }
//
//    }


    //设置Button背景颜色
    public func setBackgroundColor(_ color: UIColor, forState: UIControlState) {
        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        UIGraphicsGetCurrentContext()?.setFillColor(color.cgColor)
        UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
        let theImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.setBackgroundImage(theImage, for: forState)
    }
    
    
    //设置图片 和 标题 位置可选
    @objc public func set(image anImage: UIImage?, title: String,
                   titlePosition: UIViewContentMode, additionalSpacing: CGFloat, state: UIControlState){
        self.imageView?.contentMode = .center
        self.setImage(anImage, for: state)
        
        positionLabelRespectToImage(title: title, position: titlePosition, spacing: additionalSpacing)
        
        self.titleLabel?.contentMode = .center
        self.setTitle(title, for: state)
    }
    
    public func positionLabelRespectToImage(title: String, position: UIViewContentMode,
                                             spacing: CGFloat) {
        let imageSize = self.imageRect(forContentRect: self.frame)
        let titleFont = self.titleLabel?.font!
        let titleSize = title.size(withAttributes: [NSAttributedStringKey.font: titleFont!])
        
        var titleInsets: UIEdgeInsets
        var imageInsets: UIEdgeInsets
        
        switch (position){
        case .top:
            titleInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + spacing),
                                       left: -(imageSize.width), bottom: 0, right: imageSize.width)
            imageInsets = UIEdgeInsets(top: 0, left: titleSize.width, bottom: 0, right: -titleSize.width)
        case .bottom:
            titleInsets = UIEdgeInsets(top: (imageSize.height + spacing)/2,
                                       left: -(imageSize.width), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: -(titleSize.height + spacing)/2, left: (titleSize.width), bottom: 0, right: 0)
        case .left:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: imageSize.width)
            imageInsets = UIEdgeInsets(top: 0, left: (titleSize.width + spacing), bottom: 0,
                                       right: -(titleSize.width + spacing))
        case .right:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -spacing)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        default:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
        
        self.titleEdgeInsets = titleInsets
        self.imageEdgeInsets = imageInsets
    }
}

//MARK:快捷添加点击事件
extension UIButton{
    private struct AssociatedKeys{
        static var actionKey = "actionKey"
    }

    @objc var action : BtnBlock?{
        set{
            objc_setAssociatedObject(self,&AssociatedKeys.actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
        get{
            if let action = objc_getAssociatedObject(self, &AssociatedKeys.actionKey) as? BtnBlock{
                return action
            }
            return nil
        }
    }

    public func addClick(act:@escaping BtnBlock){
        self.action = act
        self.addTarget(self, action: #selector(touchUpInsideAction(_:)), for: .touchUpInside)
    }

    @objc private func touchUpInsideAction(_ btn : UIButton){
        self.action?(btn)
    }
}

